import sys
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.httpclient
from bot.insurance_planner_gpt.config.conf import profile_
from bot.insurance_planner_gpt.interface.planner_agent_handler import GetActiveSessionHandler, \
    GetSessionTakeoverStatusHandler, AgentLogOutHandler
from common.log import logger
import settings

from bot.insurance_planner_gpt.interface.planner_wx_handler import GetWxInfoHandler
from bot.insurance_planner_gpt.interface.planner_conversation_handler import FeedbackHandler, \
    DownLoadUserSessionConversationHandler, \
    GetSessionIdByUserHandler, GetConversationBySessionIdHandler, GetConversationHandler

arg = sys.argv

# 设置 SSL 配置
ssl_options = {
    "certfile": './common/ssl/socket.chatlingxi.com.pem',
    "keyfile": './common/ssl/socket.chatlingxi.com.key',
}

if __name__ == '__main__':

    _routes = [
        (r"/feedback", FeedbackHandler),
        (r"/download_user_conversation", DownLoadUserSessionConversationHandler),
        (r"/getSessionIdsByUser", GetSessionIdByUserHandler),
        (r"/getConversationBySessionId", GetConversationBySessionIdHandler),
        (r"/get_conversation", GetConversationHandler),
        (r"/getWxInfo", GetWxInfoHandler),
        (r"/getActiveSession", GetActiveSessionHandler),
        (r"/getSessionTakeoverStatus", GetSessionTakeoverStatusHandler),
        (r"/agent_log_out", AgentLogOutHandler),
    ]

    app = tornado.web.Application(handlers=_routes, debug=False)
    if settings.ONLINE:
        http_server = tornado.httpserver.HTTPServer(app, ssl_options=ssl_options)
    else:
        http_server = tornado.httpserver.HTTPServer(app)
    port = profile_['web_server']['port']
    http_server.bind(port)
    logger.info(f"********** server started on port:{port} **********")
    process_num = profile_['web_server']['process_num']
    http_server.start(process_num)
    tornado.ioloop.IOLoop.current().start()
